export async function getPerformanceMetrics(page) {
  const metrics = await page.evaluate(() => {
    return new Promise((resolve) => {
      // 使用 PerformanceObserver 监听导航性能指标
      const observer = new PerformanceObserver((list) => {
        observer.disconnect();
        
        // 获取绘制时间指标
        const paintEntries = performance.getEntriesByType('paint');
        const firstPaint = paintEntries.find(entry => entry.name === 'first-paint');
        const firstContentfulPaint = paintEntries.find(entry => entry.name === 'first-contentful-paint');

        // 获取导航时间指标
        const navigationTiming = performance.timing || {};
        const navStart = navigationTiming.navigationStart || 0;

        // 获取内存使用情况
        const memory = performance.memory ? {
        //   usedJSHeapSize: Math.round(performance.memory.usedJSHeapSize / (1024 * 1024)),
          totalJSHeapSize: Math.round(performance.memory.totalJSHeapSize / (1024 * 1024)),
          jsHeapSizeLimit: Math.round(performance.memory.jsHeapSizeLimit / (1024 * 1024))
        } : null;

        resolve({
          // 使用 paint timing
          firstPaint: firstPaint ? firstPaint.startTime : 0,
          firstContentfulPaint: firstContentfulPaint ? firstContentfulPaint.startTime : 0,
          // 使用 navigation timing
          domContentLoaded: navigationTiming.domContentLoadedEventEnd - navStart,
          loadComplete: navigationTiming.loadEventEnd - navStart,
          // 内存信息
          memory,
        });
      });

      // 开始观察导航性能指标
      observer.observe({ entryTypes: ['navigation', 'paint'] });

      // 添加直接获取 paint timing 的方式
      const getPaintMetrics = () => {
        const paintEntries = performance.getEntriesByType('paint');
        const firstPaint = paintEntries.find(entry => entry.name === 'first-paint');
        const firstContentfulPaint = paintEntries.find(entry => entry.name === 'first-contentful-paint');
        
        return {
          firstPaint: firstPaint ? firstPaint.startTime : null,
          firstContentfulPaint: firstContentfulPaint ? firstContentfulPaint.startTime : null
        };
      };

      // 如果5秒后还没有数据，使用备用方案
      setTimeout(() => {
        observer.disconnect();
        const timing = performance.timing;
        const navStart = timing.navigationStart;
        const paintMetrics = getPaintMetrics();
        
        // 获取内存使用情况（备用方案中也包含）
        const memory = performance.memory ? {
        //   usedJSHeapSize: Math.round(performance.memory.usedJSHeapSize / (1024 * 1024)),
          totalJSHeapSize: Math.round(performance.memory.totalJSHeapSize / (1024 * 1024)),
          jsHeapSizeLimit: Math.round(performance.memory.jsHeapSizeLimit / (1024 * 1024))
        } : null;

        resolve({
          firstPaint: paintMetrics.firstPaint || 0,
          firstContentfulPaint: paintMetrics.firstContentfulPaint || 0,
          domContentLoaded: timing.domContentLoadedEventEnd - navStart,
          loadComplete: timing.loadEventEnd - navStart,
          memory,
        });
      }, 5000);
    });
  });

  return {
    ...metrics,
    memoryInfo: metrics.memory
  };
}
